//
//  ImageCell.m
//  MoKe
//
//  Created by hua zhang on 3/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ImageCell.h"

@interface ImageCell ()

@property (nonatomic, retain) UIImageView *bgImageView;

@end

@implementation ImageCell
@synthesize imageView = _imageView;
@synthesize selected = _selected;
@synthesize bgImageView = _bgImageView;

- (void)dealloc {
    RELEASE_SAFELY(_bgImageView);
    RELEASE_SAFELY(_imageView);
}

- (id)initWithReuseIdentifier:(NSString *)inReuseIdentifier {
    self = [super initWithFrame:CGRectMake(0, 0, kDefaultImageCellWidth, kDefaultImageCellHeight) 
                reuseIdentifier:inReuseIdentifier];
    if (self) {
        self.selected = NO;
    }
    
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];
    
    if (_imageView) {
//        [self.imageView resetImage];
    }
    self.selected = NO;
}

//- (void)layoutSubviews {
//    [super layoutSubviews];
//    
//    [self sendSubviewToBack:self.bgImageView];
//}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(7, 7,
                                                                   kDefaultImageCellWidth-14, kDefaultImageCellHeight-14)];
        _imageView.backgroundColor = [UIColor clearColor];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.clipsToBounds = YES;
        [self addSubview:_imageView];
    }
    return _imageView;
}

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _bgImageView.backgroundColor = [UIColor clearColor];
        _bgImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        _bgImageView.clipsToBounds = YES;
        [self addSubview:_bgImageView];
        [self sendSubviewToBack:_bgImageView];
    }
    return _bgImageView;
}

- (void)setSelected:(BOOL)selected {
    _selected = selected;
    if (_selected) {
        self.bgImageView.image = IMGNAMED(@"bg_imagelist_cell_highlight.png");
    } else {
        self.bgImageView.image = IMGNAMED(@"bg_imagelist_cell_normal.png");
    }
}

- (void)setImageURL:(NSString *)url {
    [self.imageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:IMGNAMED(@"bg_pic_placeholder_red.png")];
}

//- (void)buImageViewdidFinishLoad:(BUImageView *)imageView {
//    if (imageView == _imageView) {
//        if (_imageView.image) {
//            
//            CABasicAnimation *opAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
//            opAnim.duration = 1;
//            opAnim.fromValue = [NSNumber numberWithFloat:0.5];
//            opAnim.toValue = [NSNumber numberWithFloat:1.0];
//            opAnim.removedOnCompletion = YES;
//            [opAnim setValue:@"fadeInAnim" forKey:@"moveAnim"];
//            opAnim.fillMode = kCAFillModeForwards;
//            
//            [_imageView.layer addAnimation:opAnim forKey:@"moveAnim"];
//        }
//    }
//}

@end
